package org.obeonetwork.dsl.spem.extension.ifest.Actions;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.obeonetwork.dsl.spem.extension.ifest.ifest.IFEST;
import org.obeonetwork.dsl.spem.extension.ifest.ontology.OWLIMTool;

import fr.obeo.dsl.viewpoint.tools.api.ui.IExternalJavaAction;

public class ImportFromOntologyAction implements IExternalJavaAction {
	public IFEST element = null;
	public boolean canExecute(Collection<? extends EObject> arg0) {
		// TODO Auto-generated method stub
		if (arg0.size() != 1) return false;
		EObject o = arg0.iterator().next();		
		if (! (o instanceof IFEST)) return false;
		element = (IFEST)o;
		return true;
	}

	public void execute(Collection<? extends EObject> arg0,
			Map<String, Object> arg1) {
		// TODO Auto-generated method stub
		//get context list from ontology repository
		List<String> lstContext = OWLIMTool.getOntologyList();
		//get active windows
		Shell page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		//create dialog
		ElementListSelectionDialog dlg = new ElementListSelectionDialog(page, new LabelProvider());
		dlg.setElements(lstContext.toArray());
		dlg.setTitle("Choose a context to import");
		dlg.create();
		if (dlg.open()==Window.OK)
		{
			String select_context = (String)dlg.getResult()[0];
			List<String> result = OWLIMTool.getOWL(select_context);
			ElementListSelectionDialog dlg1 = new ElementListSelectionDialog(dlg.getShell(), new LabelProvider());
			dlg1.setElements(result.toArray());
			dlg1.setTitle("Choose the elements to import");
			dlg1.create();
			if (dlg1.open()==Window.OK)
			{
				
			}
//			try
//			{
//				OWLIMTool.getOWL(select_context, "D://Working/Ontologies/temp.owl");
//				MessageBox messbox = new MessageBox(page);
//				messbox.setText("Import result:");
//				messbox.setMessage("Successfully");
//				
//				messbox.open();
//				OntologyConverter converter = new OntologyConverter();
//				IFEST rs = converter.Ontology2IFEST("D://Working/Ontologies/temp.owl");
//				element.getEngineeringDomain().addAll(rs.getEngineeringDomain());
//			}
//			catch(Exception e)
//			{
//				MessageBox messbox = new MessageBox(page);
//				messbox.setMessage("Error --> "+e.getMessage());
//				messbox.setText("Import result:");
//				messbox.open();
//			}
			
			
			
			
		}
	}

}
